<?php

namespace Istat;


class Importer
{

	protected $service_url = "http://apistat.istat.it/?q=getdim&lang=0&dataset=";
        protected $services = array();
        protected $browser;
	public $data_set;
	protected $cities = array();
	public $type = 0;
	public $weight = 0;

	// importazioni
     	public $citta;
        public $anno;
        public $geo = array();
        public $value = array();
	public $normalized_score = array();

        public function __construct($browser = null)
        {
                $this->browser = $browser;
        }

	public function importCities($cities)
	{
		$dump = json_decode(file_get_contents($cities));
			
		foreach ($dump as $k => $v)
		{
			$this->cities[$k] = $v;
		}
		
	}

	public function geocode($city_name)
        {

                $lat = $lon = 0;

                if (!isset($this->cities[$city_name]))
                {

                    $request = $this->browser->get('http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address='.$city_name.'+Italy');
                    $city = json_decode($request->getContent(), true);

                    if (isset($city['results'][0]))
                    {
                        $lat = $city['results'][0]['geometry']['location']['lat'];
                        $lon = $city['results'][0]['geometry']['location']['lng'];

                        $this->cities[$city_name] = array($lat, $lon);
                    }
                    else {
                        echo "non mi connetto a google!";
                    }
                }
                else {
                    $lat = $this->cities[$city_name][0];
                    $lon = $this->cities[$city_name][1];
                }

                return array($lat, $lon);
        }


        public function generateUrls($geo = false)
        {
                $response = $this->browser->get($this->service_url.$this->data_set);
                $services = json_decode($response->getContent(), true);

                $this->services = $this->sanitize($services);

                $parsed = array();
                foreach ($this->services['Territorio'] as $place)
                {
                        if ($geo) $place['geo'] = $this->geocode($place['Desc']);

                        foreach($this->services['Anno'] as $anno)
                        {

                                $parsed[] = 'http://apistat.istat.it/?q=getdatajson&dataset='.$this->data_set.'&dim='.$place['Cod'].',0,'.$anno['Cod'];

                        }
                }

                return $parsed;
        }


        public function getDataSet()
        {
                return $this->data_set;
        }

        public function sanitize($data)
        {
                foreach ($data as $key => $value)
                {
                        foreach ($value as $k => $service)
                        {
                                array_walk($service, create_function('&$var', '$var = trim($var);'));
                                $value[$k] = $service;
                        }
                        $data[$key] = $value;
                }

                return $data;
        }

	public function setService($service)
	{
		$this->service_url = $service;
	}


	public function savePath($savePath = './urls', $suffix = '')
	{
		return fopen($savePath.'/'.$this->getDataSet().$suffix.'.txt', 'a');
	}
	
	public function calculateScore()
	{
		
	}
}


